/*
 * MComboBox的测试类
 * and open the template in the editor.
 */
package org.vacoor.ui.qq.combobox.demo;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.vacoor.ui.qq.combobox.ItemListener;
import org.vacoor.ui.qq.combobox.QQComItem;
import org.vacoor.ui.qq.combobox.QQComboBox;

/**
 * 本类为MComboBox的测试类
 * 
 * @author Vacoor
 */
public class ComDemo extends JFrame implements ActionListener, ItemListener {

	private static final long serialVersionUID = 1L;
	private JPanel panel;
    private String[] name;
    private String[] num;
    private Icon[] icon;
    private QQComItem[] items;
    private QQComboBox comboBox;
    private boolean isShow;
    private JButton button;
    private final QQComboBox editComboBox;

    public ComDemo() {
//        this.setFeel();
        isShow = true;
//        this.setUndecorated(true);
        this.setTitle("MComboBox'Test");
        name = new String[]{"测试1", "测试2", "测试3", "测试4", "测试5"};
        num = new String[]{"1234567891", "1234567892", "1234567893", "1234567894", "1234567895", "123无姓名测试", "123仅号码测试"};
        icon = new ImageIcon[]{
            new ImageIcon(this.getClass().getClassLoader().getResource("images/comboboxtest/0.png")),
            new ImageIcon(this.getClass().getClassLoader().getResource("images/comboboxtest/1.png")),
            new ImageIcon(this.getClass().getClassLoader().getResource("images/comboboxtest/2.png")),
            new ImageIcon(this.getClass().getClassLoader().getResource("images/comboboxtest/3.png")),
            new ImageIcon(this.getClass().getClassLoader().getResource("images/comboboxtest/4.png")),
            new ImageIcon(this.getClass().getClassLoader().getResource("images/comboboxtest/5.png"))};
        items = new QQComItem[]{
            new QQComItem(icon[0], name[0], num[0]),
            new QQComItem(icon[1], name[1], num[1]),
            new QQComItem(icon[2], name[2], num[2]),
            new QQComItem(icon[3], name[3], num[3]),
            new QQComItem(icon[4], name[4], num[4]),
            new QQComItem(icon[5], num[5]),
            new QQComItem(num[6])
        };
        comboBox = new QQComboBox(items);
        comboBox.setFieldIconShow(isShow);
        comboBox.setEditable(false);
        comboBox.setNoItemText("没有选项时的文本");
        comboBox.addMItemListener(this);
        button = new JButton("显示/隐藏输入框图标");
        button.addActionListener(this);
        //
        editComboBox = new QQComboBox(items);
        editComboBox.setFieldIconShow(isShow);
        editComboBox.setEditable(true);
        editComboBox.setNoItemText("没有选项时的文本");
        editComboBox.addMItemListener(this);
        button = new JButton("显示/隐藏输入框图标");
        button.addActionListener(this);
        panel = new JPanel(new BorderLayout());
        panel.add(comboBox, "North");
        panel.add(editComboBox, "Center");
        panel.add(button, "South");
        panel.setBackground(Color.white);
        this.add(panel);
        this.setDefaultCloseOperation(3);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setAlwaysOnTop(true);
    }

    //设置外观风格
    private void setFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println("异常:" + e);
        }
    }

    //以下为接口方法
    @Override
    public void actionPerformed(ActionEvent e) {
        comboBox.setFieldIconShow(!isShow);
        editComboBox.setFieldIconShow(!isShow);
        isShow = !isShow;
    }

    @Override
    public boolean itemDeleting(QQComItem item) {
        return true;
    }

    @Override
    public void itemDeleted(QQComItem item) {
        System.out.println("您删除了:" + item.getName() + "," + item.getNum());
    }

    @Override
    public void itemSelected(QQComItem item) {
        System.out.println("您选择了:" + item.getName() + "," + item.getNum());
    }

    /*
     * 
     */
    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ComDemo().setEnabled(true);
            }
        });
    }
}
